home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vol7n7.arc / PP707.ARC / HTOL.ASM next >
Assembly Source File  |  1987-12-15  |  3KB  |  85 lines

  1.         name    HTOL
  2.         page    55,132
  3.         title   HTOL - Hex ASCII to long integer
  4.  
  5. ;
  6. ; HTOL.ASM - convert hexadecimal ASCII string 
  7. ;            to long (32-bit) integer.
  8. ;
  9. ; Copyright (C) 1988 Ziff Communications Co.
  10. ; PC Magazine * Ray Duncan
  11. ; Call with:    DS:SI = address of string
  12. ;
  13. ; Returns:      DX:AX = result (high word in DX)
  14. ;               DS:SI = address+1 of terminator 
  15. ;
  16. ;               other registers preserved
  17. ;
  18. ; Like the C library 'atol', this routine gives no 
  19. ; warning in the event of overflow, and terminates 
  20. ; on the first unconvertable character.
  21. ;
  22.  
  23. _TEXT   segment word public 'CODE'
  24.  
  25.         assume  cs:_TEXT
  26.  
  27.         public  htol                    ; make HTOL available
  28.                                         ; to the Linker
  29.  
  30. htol    proc    near                    ; Convert hex ASCII string
  31.                                         ; to 32-bit binary integer.
  32.  
  33.         push    cx                      ; save register
  34.         xor     cx,cx                   ; set forming answer to zero
  35.         xor     dx,dx
  36.  
  37. htol1:  lodsb                           ; get next char., make sure
  38.                                         ; it is '0'-'9' or 'A'-'F'
  39.  
  40.         cmp     al,'0'
  41.         jb      htol3                   ; exit if char < '0'
  42.  
  43.         cmp     al,'9'
  44.         jbe     htol2                   ; proceed if char '0'-'9'
  45.  
  46.         or      al,20h                  ; fold char. to lower case.
  47.  
  48.         cmp     al,'f'
  49.         ja      htol3                   ; exit if > 'F' or 'f'
  50.  
  51.         cmp     al,'a'
  52.         jb      htol3                   ; exit if < 'A' or 'a'
  53.  
  54.         add     al,9                    ; else add fudge factor
  55.                                         ; for digits 'A'-'F'
  56.  
  57. htol2:                                  ; add this digit to
  58.                                         ; forming answer...
  59.  
  60.         shl     cx,1                    ; first shift current answer 
  61.         rcl     dx,1                    ; left by 4 bits...
  62.         shl     cx,1
  63.         rcl     dx,1            
  64.         shl     cx,1
  65.         rcl     dx,1            
  66.         shl     cx,1
  67.         rcl     dx,1            
  68.         and     ax,0fh                  ; isolate binary value 0-A
  69.                                         ; from ASCII character code,
  70.         or      cx,ax                   ; add to the forming answer.
  71.  
  72.         jmp     htol1                   ; get next character
  73.  
  74. htol3:  mov     ax,cx                   ; return DX:AX = value
  75.         pop     cx                      ; restore register
  76.         ret                             ; back to caller
  77.  
  78. htol    endp
  79.  
  80. _TEXT   ends
  81.  
  82.         end
  83.  
  84.